1709B - Also Try Minecraft - CodeForces Solution


data structures dp implementation

Please click on ads to support us..

Python Code:

(a, b) = tuple(map(int, input().split()))
L = list(map(int, input().split()))
L1 = [0]*a
L2 = [0]*a
for i in range(1, a):
    L1[i] = L1[i-1] + max(0, L[i-1]-L[i])
for j in range(a-2, -1, -1):
    L2[j] = L2[j+1] + max(0, L[j+1]-L[j])
for _ in range(b):
    (c, d) = tuple(map(int, input().split()))
    if d > c:

        print(abs(L1[c-1]-L1[d-1]))
    else:
        print(abs(L2[c-1]-L2[d-1]))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define MOD 998244353
#define MAX 2147483647
#define fin(i,a,b) for(int i=a;i<b;i++)
#define fdec(i,a,b) for(int i=a;i>b;i--)
#define ve(n) vector<int> v(n);
#define pr(x,y) make_pair(x,y);
bool sortbysec(const pair<int,int> &a,
  const pair<int,int> &b);

bool sortbysec(const pair<int,int> &a,
  const pair<int,int> &b)
{
    return (a.second < b.second);
}
void printVector(vector<int> v){
    for(auto i:v){
        cout<<i<<" ";
    }
    cout<<endl;
}

void printMap(map<int,int>mp){
    for(auto i:mp){
        cout<<i.first<<" -> "<<i.second<<endl;
    }
}

int factorial(int n){
    int f = 1;
    for(int i=2;i<=n;i++){
        f=(f*i)%MOD;
    }
    return f;
}

bool isPowerOfTwo(int n)
{
    if (n == 0)
        return false;

    return (ceil(log2(n)) == floor(log2(n)));
}

int gcd(int a, int b)
{
  if (a == 0)
    return b;
return gcd(b % a, a);
}

int lcm(int a, int b){
    return (a*b)/(__gcd(a,b));
}

int arrGCD(vector<int> v, int n)
{
  int result = v[0];
  for (int i = 1; i < n; i++)
  {
    result = gcd(v[i], result);

    if(result == 1)
    {
        return 1;
    }
}
return result;
}

vector<int> primeFactorization(int n) {
    vector<int> pf;
    while (n%2 == 0){
      pf.push_back(2);
      n = n/2;
  }
  for (int i = 3; i <= sqrt(n); i = i+2){
      while (n%i == 0){
        pf.push_back(i);
        n = n/i;
    }
}
if (n > 2)
  pf.push_back(n);
return pf;
}

int sumOfDigits(int n){
    int sum=0;
    while(n>0){
        sum+=n%10;
        n/=10;
    }
    return sum;
}

int countDigit(int n) {
  return floor(log10(n) + 1);
}

vector<int> decToBinary(int n)
{
    vector<int> binaryNum(32);
    int i = 0;
    while (n > 0) {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    return binaryNum;
}

void solve(){
    int n,m;
    cin>>n>>m;
    ve(n);
    fin(i,0,n){
        cin>>v[i];
    }
    int sum=0;
    vector<int>l(n);
    l[0] = 0;
    fin(i,1,n){
        if(v[i-1]>v[i]){
            sum+=v[i-1]-v[i];
        }
        l[i] = sum;
    }
    int s2=0;
    vector<int>r(n);
    r[n-1] = 0;
    for(int i=n-2;i>=0;i--){
        if(v[i]<v[i+1]){
            s2+=v[i+1]-v[i];
        }
        r[i] = s2;
    }
    fin(i,0,m){
        int x,y;
        cin>>x>>y;
        if(y>=x){
            cout<<l[y-1]-l[x-1]<<endl;
        } else {
            cout<<r[y-1]-r[x-1]<<endl;
        }
    }
}


int32_t main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r",stdin); 
    freopen("output.txt", "w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    /*int t;
    cin>>t;
    while(t--)
    {
        solve();
    }*/
    solve();

    return 0;
}


Comments

Submit
0 Comments
More Questions

1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold